/*
 * Copyright (c) 2019 Winner Microelectronics Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-02-13     tyx          first implementation
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <fal.h>
#include <drv_ch423.h>
#include <beep.h>

#include <ext_gpio_control_thread.h>

extern void wlan_autoconnect_init(void);

extern void
netbiosns_set_name(const char *hostname);
extern void
netbiosns_init(void);
extern int wol_thread_create(void);

static void
wifi_ready_callback(int event, struct rt_wlan_buff *buff, void *parameter)
{
	rt_thread_mdelay(2000);
	rt_kprintf("%s\n", __FUNCTION__);
	wol_thread_create();
}

int main(void)
{
	rt_wlan_register_event_handler(RT_WLAN_EVT_READY,
								   wifi_ready_callback, RT_NULL);
	/* set wifi work mode */
	rt_wlan_set_mode(RT_WLAN_DEVICE_STA_NAME, RT_WLAN_STATION);
	rt_wlan_set_mode(RT_WLAN_DEVICE_AP_NAME, RT_WLAN_AP);

	/* init Wi-Fi auto connect feature */
	wlan_autoconnect_init();
	/* enable auto reconnect on WLAN device */
	rt_wlan_config_autoreconnect(RT_TRUE);

	netbiosns_set_name("nixieclock");
	netbiosns_init();

	beep_init(BEEP_PIN, PIN_LOW);

	set_speech_module_power_on();
	set_speech_module_uart_to_mcu();

	while (1)
	{
		rt_thread_mdelay(500);
	}
	return 0;
}

#include "sensor_asair_aht20_f.h"
#define AHT10_I2C_BUS "i2c2soft"

int rt_hw_aht10_port(void)
{
	struct rt_sensor_config cfg;

	cfg.intf.dev_name = AHT10_I2C_BUS;
	cfg.intf.user_data = (void *)AHT20_F_I2C_ADDR;

	rt_hw_aht20_f_init("aht20_f", &cfg);

	return RT_EOK;
}
INIT_ENV_EXPORT(rt_hw_aht10_port);

static int test_cmd(int argc, char **argv)
{
	int ret = 0;
	if (!rt_strcmp(argv[1], "speech_power_on"))
	{
		set_speech_module_power_on();
	}
	else if (!rt_strcmp(argv[1], "speech_power_off"))
	{
		set_speech_module_power_off();
	}
	else if (!rt_strcmp(argv[1], "speech_uart_to_mcu"))
	{
		set_speech_module_uart_to_mcu();
	}
	else if (!rt_strcmp(argv[1], "speech_uart_to_usb2ttl"))
	{
		set_speech_module_uart_to_usb2ttl();
	}
	else
	{
		rt_kprintf("Please test_cmd<speech_power_on|speech_power_off|speech_uart_to_mcu|speech_uart_to_usb2ttl>'\n");
	}
	return ret;
}
MSH_CMD_EXPORT(test_cmd, test cmd);

static int speech_module_upgrade(int argc, char **argv)
{
	set_speech_module_power_off();
	set_speech_module_uart_to_usb2ttl();
	rt_thread_mdelay(1000);
	set_speech_module_power_on();
}
MSH_CMD_EXPORT(speech_module_upgrade, speech module upgrade);